package main;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import br.cin.stadium.middleware.network.config.NetConfig;
import br.cin.stadium.middleware.util.Logger;

public class ResponseBroker {

	private ServerSocket serverSocket;
	private Socket socket;

	public ResponseBroker() {
		try {
			serverSocket = new ServerSocket(NetConfig.STADIUM_DIRECTOR_PORT_RETRIEVE);

		} catch (IOException e) {
			Logger.log("Cannot create the server socket on port " + NetConfig.STADIUM_DIRECTOR_PORT_RETRIEVE + ".");
			e.printStackTrace();
			System.exit(-1);
		}
	}

	public void listen() {
		try {
			Logger.log("ResponseBrocker starts to listen !!!");
			socket = serverSocket.accept();
			Logger.log("New connection to respondeBroker !!!");

		} catch (IOException e) {
			Logger.log("ResponseBroker - Cannot accept the connection on the server socket.");
			//e.printStackTrace();
			System.exit(-1);
		}
	}

	public void close() {
		try {
			socket.close();
		} catch (IOException e) {
			Logger.log("Cannot close the socket.");
			e.printStackTrace();
			System.exit(-1);
		}
	}

	public void response(byte[] bytes) {
		try {
			socket.getOutputStream().write(bytes);
		} catch (IOException e) {
			Logger.log("Network is not available !");
		}
	}
}